{% extends 'index.html' %}
{% block subtitle %}- 登录页{% endblock %}

<!--head-->
{% block head %}
    <script >
      function checkUser() {
          var username = document.getElementById("username").value;
          var password = document.getElementById("password").value;
          if (username == "") {
              alert("账号不能为空")
          }
          else if (username.length > 8) {
              alert("用户名只能为1-8个字符/汉字的长度")
          }
          else if (password == "") {
              alert("密码不能为空")
          }
          else if (password.length > 12) {
              alert("密码只能为1-12个字符/汉字的长度")
          }
          else {
              var data = {
                  data: JSON.stringify({
                      "username": username,
                      "password": password,
                  }),
              }
              $.ajax({
                  url: "/login/",
                  type: "post",
                  data: data,
                  datatype: "json",
                  success: function (data) {
                      if (data['code'] === 200) {
                          alert("登录成功")
                          window.location.href = "{{ url_for('main.index') }}"
                      } else if (data['code'] === 400) {
                          alert("账号不存在")
                      } else {
                          alert("密码错误")
                      }
                  },
                  error: function (error) {
                      console.log(error)
                      alert("未知错误")
                  }
              })
          }
      }
    </script>
{% endblock %}

{% block content %}
<div class="login_banner">
    <div id="content">
        <div class="login_form">
            <div class="login_box">
                <div class="tit">
                    <h1>农产品订购登录</h1>
                    <a href="/register/")>立即注册</a>
                </div>
                <br>
                <div class="form">
                        <form>
                        <label>用户名称：</label>
                        <input class="itxt"   type="text" placeholder="请输入用户名" autocomplete="on" tabindex="1" id="username" name="username" style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: solid"/>
                        <br />
                        <br />
                        <label>用户密码：</label>
                        <input class="itxt"  type="password" placeholder="请输入密码" autocomplete="on" tabindex="1" id="password" name="password" style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: solid"/>
                        <br />
                        <br />
                            <input type="button"  value="登录" id="sub_btn" onclick="checkUser()"/>
                        </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}



